package com.yiren.manager.ctrl.cost;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.type.TypeReference;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FavoriteSet;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderArrearage;
import com.yiren.entity.YrElderBeHospital;
import com.yiren.entity.YrElderPayRecord;
import com.yiren.entity.YrElderWallet;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.ctrl.elder.YrElderBaseController;
import com.yiren.manager.form.cost.YrArrearagePayCostForm;
import com.yiren.manager.form.cost.YrElderArrearageForm;
import com.yiren.manager.form.elder.YrElderBeHospitalForm;
import com.yiren.manager.service.iface.cost.YrElderArrearageService;
import com.yiren.manager.service.iface.elder.YrElderBeHospitalService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.elder.YrElderWalletService;
import com.yiren.manager.utils.ObjectUtils;

/**
 * 欠费补交
 * @author yanwenhao
 *
 */
@Controller
@RequestMapping("elderArrearage")
public class YrElderArrearageController extends YrElderBaseController<YrElderArrearageForm, YrElderArrearage>{

	@Autowired
	private YrElderArrearageService selfService;
	
	@Autowired
	private YrElderWalletService elderWalletService;
	
	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrElderBeHospitalService elderBeHospitalService;
	
	@RequestMapping("updateBetch")
	@ResponseBody
	public HTTPResult<YrElderArrearage> updateBetch(String data) throws SwallowException, MyException{
		HTTPResult<YrElderArrearage> result = new HTTPResult<YrElderArrearage>();
		JsonMapperTool tool = new JsonMapperTool(data);
		List<YrElderArrearage> form = tool.mapper("elderArrearageList", new TypeReference<ArrayList<YrElderArrearage>>() {});
		
		this.selfService.updateBetch(form);
		return result;
	}
	
	
	/**
	 * 欠费补交
	 * @return
	 * @throws SwallowException 
	 * @throws MyException 
	 */
	@RequestMapping("payCost")
	@ResponseBody
	public HTTPResult<YrArrearagePayCostForm> payCost(String data) throws SwallowException, MyException{
		HTTPResult<YrArrearagePayCostForm> result = new HTTPResult<YrArrearagePayCostForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		
		YrArrearagePayCostForm form = tool.mapper("arrearagePayCostForm", YrArrearagePayCostForm.class);
		
		if(null==form){
			throwExp("参数有误");
		}
		
		YrElder elder = elderService.findOne(form.getElderId());
		if(null==elder){
			throwExp("查询不到该老者的相关信息");
		}
		
		String sqlWhere = "elder_id=?1 AND be_status<>2";
		
		QueryResult<YrElderBeHospital> qrElderBeHospital = elderBeHospitalService.find(YrElderBeHospital.class, sqlWhere, new Object[]{form.getElderId()});
		if(qrElderBeHospital.getResultList().isEmpty()){
			throwExp("查询不到相关老者住院登记信息");
		}
		YrElderBeHospital elderBeHospital = qrElderBeHospital.getResultList().get(0);
		YrElderWallet elderWallet = null;
		YrElderPayRecord elderPayRecord = null;
		
		if(form.getPayWay()==2){//缴费方式为钱包
			 sqlWhere = "elder_id=?1";
			
			QueryResult<YrElderWallet> qrElderWallet = elderWalletService.find(YrElderWallet.class,sqlWhere, new Object[]{form.getElderId()});		
		
			if(qrElderWallet.getTotalRecord()==0){
				throwExp("查询不到该老者的钱包");
			}
			elderWallet = qrElderWallet.getResultList().get(0);
			if(elderWallet.getWalletBalance()-form.getPayMoney()<0){
				throwExp("补交失败，钱包余额不足");
			}
			elderWallet.setWalletBalance(elderWallet.getWalletBalance()-form.getPayMoney());
		}
		
		List<YrElderPayRecord> elderPayRecords = new ArrayList<YrElderPayRecord>();
		
		List<YrElderArrearage> elderArrearages = new ArrayList<YrElderArrearage>();
		
		for(String id : form.getPayIdList()){
			YrElderArrearage elderArrearage = selfService.findOne(id);
			elderArrearage.setStatus(1);
			elderArrearage.setArrearagePayWay(form.getPayWay());
			//增加一条缴费记录
			elderPayRecord = costElderPayRecord(elderBeHospital.getBeHospitalId(),elderArrearage.getArrearageDesc(), elder, elderArrearage.getArrearageType(), "-1", elderArrearage.getArrearageMoney(), form.getPayWay(),1,form.getCreateId(),1);

			elderArrearages.add(elderArrearage);
			elderPayRecords.add(elderPayRecord);
		}
		
		FavoriteSet favoriteSet = new FavoriteSet();
		favoriteSet.putFavorite("elderArrearages", elderArrearages);
		favoriteSet.putFavorite("elderWallet", elderWallet);
		favoriteSet.putFavorite("elderPayRecords", elderPayRecords);
		
		selfService.executePayCost(favoriteSet);
		
		return result;
	}
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrElderArrearageForm> find(String searchVal) throws Exception{
		 HTTPResult<YrElderArrearageForm> result = new HTTPResult<YrElderArrearageForm>();
		 
		 String sqlWhere = "(elder_name=?1 OR elder_card_id=?2) AND status=0";
		 LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		 sortMap.put("createTime", "DESC"); 
		 ArrayList<YrElderArrearageForm> formList = new ArrayList<YrElderArrearageForm>();
		 QueryResult<YrElderArrearage> qr = selfService.find(YrElderArrearage.class, sqlWhere, new Object[]{searchVal,searchVal},sortMap);
		 
		 for(YrElderArrearage entity : qr.getResultList()){
			 formList.add(toForm(entity));
		 }
		 
		 result.setData(formList);
		 
		 return result;
	}
	
	@RequestMapping("arrearageRemind")
	@ResponseBody
	public HTTPResult<YrElderArrearageForm> arrearageRemind() throws Exception{
		HTTPResult<YrElderArrearageForm> result = new HTTPResult<YrElderArrearageForm>();
		String sqlWhere = "status=0";
		LinkedHashMap<String, String> sortMap = new LinkedHashMap<>();
		sortMap.put("createTime", "DESC"); 
		QueryResult<YrElderArrearage> qr = selfService.find(YrElderArrearage.class, sqlWhere, new Object[]{},sortMap);
		
		List<YrElderArrearageForm> formList = new ArrayList<YrElderArrearageForm>();
		for(YrElderArrearage entity:qr.getResultList()){
			YrElderArrearageForm form = toForm(entity);
			sqlWhere = "elder_id=?1 AND be_status<>2";
			YrElderBeHospital elderBeHospital = elderBeHospitalService.findOne(entity.getBeHospitalId());
			if(null!=elderBeHospital){
				YrElderBeHospitalForm elderBeHospitalForm = ObjectUtils.objectToObject(elderBeHospital,YrElderBeHospitalForm.class);
				form.setElderBeHospitalForm(elderBeHospitalForm);
			}
			
			formList.add(form);
		}
		
		result.setData(formList);
		return result;
		
	}
	@Override
	public YrElderArrearageForm toForm(YrElderArrearage e) throws Exception {
		YrElderArrearageForm form = ObjectUtils.objectToObject(e, YrElderArrearageForm.class);
		return form;
	}

	@Override
	public YrElderArrearage toEntity(YrElderArrearageForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
